package logic

import (
	"context"

	"game/api/internal/svc"
	"game/api/internal/types"
	"game/rpc/gamerpc/game"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserLogic {
	return &GetUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserLogic) GetUser(req *types.GetUserRequest) (*types.GetUserResponse, error) {
	// 调用 rpc 服务的查询用户方法
	rpcReq := &game.GetUserRequest{
		Id: req.Id,
	}

	rpcResp, err := l.svcCtx.GameRpc.GetUser(l.ctx, rpcReq)
	if err != nil {
		return nil, err
	}

	// 构造 api 响应
	apiResp := &types.GetUserResponse{
		User: &types.User{
			Id:       rpcResp.User.Id,
			Username: rpcResp.User.Username,
			Email:    rpcResp.User.Email,
		},
	}

	return apiResp, nil
}
